package com.foo.AnalyzedDataProvider;

import java.sql.Date;
import java.sql.Timestamp;

import javax.ejb.Stateless;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.foo.DAO.AnalyzedDAO;
import com.foo.DataProvider.ProviderInterface;
import com.foo.DataSender.DataSender;
import com.foo.Entity.RealTimeData;
 
/**
 * Session Bean implementation class AnalyzedData
 */
@Stateless
public class AnalyzedDataProvider implements ProviderInterface{ //AnalyzedDataProviderRemote, AnalyzedDataProviderLocal {
 
	int total =0 ;
	String analyzedmessage = ""; 
	DataSender datasender = new DataSender();
	
    public AnalyzedDataProvider() 
    {
    	System.out.println("Analyzed Data Provider created!"); 
    }

	@Override
	public void getData(String sorttype, String value, Timestamp timestamp, Date fromdate, Date todate) {
		System.out.println("Entering server function Analyzed Data with parameters " +sorttype);

		System.out.println("Server side: if condition :");
		// Example for using named query
		analyzedmessage = ""; 
		ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
		AnalyzedDAO forumDAO = (AnalyzedDAO) context.getBean("analyzedDAO");   		
		RealTimeData[] datalist=forumDAO.selectForum(sorttype);		
	
		if(datalist != null)
		{
		for(int k=0 ; k< datalist.length ; k++)
		{
			analyzedmessage += datalist[k].getTicker_symbol()+","+ datalist[k].getOpen_value() +"," +datalist[k].getHigh_value() 
			+","+datalist[k].getLow_value()+ ","+datalist[k].getClose_value()+","+datalist[k].getVolume()+","+datalist[k].getPerc_change()+",";

		}
		}
		else
		{
			analyzedmessage="";
		}
		
	System.out.println("The message " +analyzedmessage);
	datasender.sendMessage("Spatial",analyzedmessage);
		
	}
}


